package com.example.myapplication2;
//C:\Users\liuzc\Desktop\MyApplication2\app\src\main\java\com\example\myapplication2\MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.widget.Toolbar;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private ChatAdapter chatAdapter;
    private List<String> messageList;
    private EditText etMessage;
    private Button btnSend;

    private Button button1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        recyclerView = findViewById(R.id.recyclerView);
        etMessage = findViewById(R.id.etMessage);
        btnSend = findViewById(R.id.btnSend);

        button1=findViewById(R.id.button_one);

        messageList = new ArrayList<>();
        chatAdapter = new ChatAdapter(messageList);

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(chatAdapter);


        btnSend.setOnClickListener(v -> {
            String userMessage = etMessage.getText().toString().trim();
            if (!userMessage.isEmpty()) {
                messageList.add(userMessage);
                Api api = new Api();
                String output=api.sendMessage(MainActivity.this,userMessage);
                messageList.add(output); // 模拟机器人的回复

                chatAdapter.notifyDataSetChanged();
                etMessage.setText(""); // 清空输入框
                recyclerView.scrollToPosition(messageList.size() - 1); // 滚动到底部
            }
        });
        //---------------------测试用按钮---------------------
//        button1.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                new Thread(new Runnable() {
//                    @Override
//                    public void run() {
//                        Api api = new Api();
//                        try {
//                            api.sendMessage(MainActivity.this);
//
//                            // 注意：这里需要修改Api类，使其能返回响应内容或直接在这里处理响应
//                            // 直接在onResponse里处理Toast显示
//                        } catch (IOException e) {
//                            e.printStackTrace();
//                            runOnUiThread(new Runnable() {
//                                @Override
//                                public void run() {
//                                    Toast.makeText(MainActivity.this, "请求失败", Toast.LENGTH_SHORT).show();
//                                }
//                            });
//                        }
//                    }
//                }).start();
//            }
//        });
//----------------------------------测试用按钮---------------------------

    }



}
